<?php

namespace api\system\controller;

class ToolController extends \Aaa\Controller {

    function t() {
        echo cmf_thinkphp_version();
        echo "\n\n";
        echo cmf_version();
    }

    //数据文档
    function table() {
        $prefix = config("database.connections.mysql.prefix");
        $table = $this->param['table'];
        $title = app()->db->query("SHOW TABLE status WHERE Name='{$prefix}{$table}'")[0]['Comment'];
        $lists = app()->db->query("show full columns from `{$prefix}{$table}`");
        //print_r($lists);
        echo '<table><caption>' . "{$title}（{$table}）" . '字段明细</caption><thead><tr class="firstRow"><th>字段</th><th>类型</th><th>说明</th></tr></thead>';
        foreach ($lists as $v) {
            echo '<tr>';
            echo "<td>$v[Field]</td>";
            echo "<td>$v[Type]</td>";
            echo "<td>$v[Comment]</td>";
            echo "</tr>\n";
        }
        echo '</table>';
        exit;
    }

    //剔除租号字段
    private function tt() {
        $list = db('renting_field')->select();
        //print_r($list);
        foreach ($list as $v) {
            $field = json_decode($v['field'], true);
            //print_r($field);
            $f = [];
            $i = 1;
            foreach ($field as $vv) {
                if (!in_array($vv['name'], [
                    '身份证',
                    '出售',
                    '出手',
                    '账号类型',
                    '账号来源',
                    '处罚记录',
                    'QQ好友',
                    'QQ等级',
                    '绑定至尊保'
                ])) {
                    $f["f{$i}"] = $vv;
                    $i++;
                }
            }
            print_r($f);
            if ($f) {
                db('renting_field')->where('id', $v['id'])->update([
                    'field' => json_encode($f, JSON_UNESCAPED_UNICODE),
                ]);
            } else {
                db('renting_field')->where('id', $v['id'])->delete();
            }

        }
    }

    //测试推送
    private function xcx() {
        $setting = cmf_get_option("system/api/unipush");
        require_once $this->rootPath . "vendor/getuilaboratory/getui-pushapi-php-client-v2/GTClient.php";
        $api = new \GTClient("https://restapi.getui.com", $setting["appKey"], $setting["appID"], $setting["masterSecret"]);

        $push = new \GTPushRequest();
        $push->setRequestId(microtime());
        $notify = new \GTNotification();
        $notify->setTitle("title");
        $notify->setBody("content");
        $message = new \GTPushMessage();
        $message->setTransmission(json_encode(['title' => 'titlexx新消息', 'content' => 'content']));
        //ios
        $ios = new \GTIos();
        $ios->setType("notify");
        //aps设置
        $aps = new \GTAps();
        $aps->setSound("com.gexin.ios.silenc");
        $alert = new \GTAlert();
        $alert->setTitle("title");
        $alert->setBody("body");
        $aps->setAlert($alert);
        $ios->setAps($aps);
        //厂商推送消息参数
        $pushChannel = new \GTPushChannel();
        $pushChannel->setIos($ios);
        $push->setPushChannel($pushChannel);

        $push->setPushMessage($message);
        $push->setCid('9a8b72780fcc239e8ecad10889f95720');
        //处理返回结果
        $result = $api->pushApi()->pushToSingleByCid($push);
        print_r($result);
    }

    //设置首字母
    private function xx() {
        $model = new \Aaa\model;
        $list = db('game_x')->select();
        foreach ($list as $v) {
            echo strtoupper($model->pinyin($v['name'], 'one'));
            echo strtoupper($model->pinyin($v['name'], 'first', ''));
            echo $v['name'];
            echo "\n";
            db('game_x')->where('id', $v['id'])->update([
                'letter' => strtoupper($model->pinyin($v['name'], 'one')),
                'initials' => strtoupper($model->pinyin($v['name'], 'first', '')),
            ]);
        }
    }

    //采集7881的分类数据 区服
    private function xxx1() {

        $item = db('game_x')->where(['flag' => 0, 'level' => 2])->order('id ASC')->find();
        $pid = $item['parent_id'];


        //$item['id_7881'] = 'A5559';
        $arr1 = file_get_contents('https://gw.7881.com/basic/api/game-detail?gameId=' . $item['id_7881']);
        $arr1 = json_decode($arr1, true)['body']['list'];
        if ($arr1) {
            foreach ($arr1 as $v1) {
                //插入
                $data = [
                    'parent_id' => $item['id'],
                    'parent_ids' => "$pid,{$item['id']}",
                    'name' => $v1['name'],
                    'id_7881' => $v1['id'],
                    'level' => 3,
                ];
                echo 11111;
                print_r($data);
                $id1 = db('game_xx')->insertGetId($data);
                if ($v1['children']) {
                    foreach ($v1['children'] as $v2) {
                        //插入
                        $data = [
                            'parent_id' => $id1,
                            'parent_ids' => "$pid,{$item['id']},$id1",
                            'name' => $v2['name'],
                            'id_7881' => $v2['id'],
                            'level' => 4,
                        ];
                        echo 2222;
                        print_r($data);
                        $id2 = db('game_xx')->insertGetId($data);
                        if ($v2['children']) {
                            foreach ($v2['children'] as $v3) {
                                //插入
                                $data = [
                                    'parent_id' => $id2,
                                    'parent_ids' => "$pid,{$item['id']},$id1,$id2",
                                    'name' => $v3['name'],
                                    'id_7881' => $v3['id'],
                                    'level' => 5,
                                ];
                                echo 33333;
                                print_r($data);
                                db('game_xx')->insertGetId($data);
                            }
                        }
                    }
                }

            }
        }
        db('game_x')->where('id', $item['id'])->update(['flag' => 1]);
        echo '<script>//setTimeout(location.reload(), 50)</script>';
        exit;
    }

    private function bbb() {//设置goods_type表
        $list = db('game')->where('level', 1)->select()->toArray();
        foreach ($list as $v) {
            db('goods_type')->insert([
                'game_id' => $v['id'],
                'name' => '账号',
                'delivery_type' => '0,1',
            ]);
            print_r($v['id'] . "\n");

        }
        exit;
    }

    private function aaa() {
        $list = db('game')->where('level', 1)->select()->toArray();
        foreach ($list as $v) {

            $list1 = db('game')->where(['level' => 2, 'parent_id' => $v['id']])->select()->toArray();
            $device = [];
            foreach ($list1 as $vv) {
                $device[] = $vv['device'];

            }
            $device = array_unique($device);
            $device[] = $v['id'];

            print_r($v['id'] . "\n");

        }
        exit;
    }


    private function aaaw() {
        $list = db('goods_type')->where('name', '金币')->select()->toArray();
        foreach ($list as $v) {
            echo $v['id'] . "\n";
            db('goods_order_field')->insert([
                'type_id' => $v['id'],
                'field' => '[{"name":"角色名称","type":"input","required":"1","repeatInput":"1","options":[]}]'
            ]);
        }
    }

    private function aaaq() {
        $list = db('game')->where('level', 1)->select()->toArray();
        foreach ($list as $v) {

            echo $v['id'] . "\n";
            db('goods_type')->insert([
                'game_id' => $v['id'],
                'name' => '金币',
                'delivery_type' => '0,1',
                'multiple' => '1',
                'built_in_type' => 'goldCoinTransaction',
                'need_review' => '0',
                'fee_pct' => '5',
                'list_order' => '1',
            ]);
            /*db('goods_account_field')->insert([
                'game_id' => $v['id'],
                'field' => '[{"name":"账号","required":"1","type":"text"},{"name":"密码","type":"text","required":"1"},{"name":"角色名称","type":"text","required":"1"}]'
            ]);*/

        }
        exit;
    }


    //采集7881的游戏分类
    private function xxx() {
        $path = CMF_ROOT . '11/';
        $files = glob($path . '*.json');
        foreach ($files as &$v) {
            $v = str_replace('/www/wwwroot/cmf/11/', '', $v);
            $v = str_replace('_.json', '', $v);
        }
        sort($files);
        foreach ($files as &$v) {
            $v = "/www/wwwroot/cmf/11/{$v}_.json";
        }
        foreach ($files as $file) {
            $arr = file_get_contents($file);
            $arr = json_decode($arr, true)['body'];
            if ($arr) {
                foreach ($arr as $game) {
                    //游戏大分类
                    $data = [
                        'name' => $game['alias'],
                        'level' => 1,
                    ];
                    $id1 = db('game_x')->insertGetId($data);

                    foreach ($game['gameList'] as $g) {
                        $os = 0;
                        if ($g['mobileGameType'] == '0') {
                            $os = 1;
                        }
                        if ($g['mobileGameType'] == '1') {
                            $os = 2;
                        }
                        if ($g['mobileGameType'] == '3') {
                            $os = 3;
                        }
                        echo $os;
                        print_r($g);

                        //游戏设备分类
                        $data = [
                            'id_7881' => $g['gameId'],
                            'name' => $g['gameName'],
                            'device' => $g['gameType'],
                            'os' => $os,
                            'is_hot' => $g['hot'] == 'hot' ? 1 : 0,
                            'parent_id' => $id1,
                            'parent_ids' => $id1,
                            'level' => 2,
                        ];
                        db('game_x')->insertGetId($data);
                    }
                }

            }
        }

        exit;

        foreach ($re as $v) {
            $idOld = $v['id'];
            unset($v['id']);
            $v['status'] = 1;
            $id = db('game_category')->insertGetId($v);
            //copy(CMF_ROOT . 'xx/' . $idOld . '.png', CMF_ROOT . 'xxx/' . $id . '.png');
            //exit;
        }

        exit;
    }

    //采集游戏图片
    private function tu() {
        $imgs = [];
        $list = db('game_x')->where('level', 2)->select()->toArray();
        foreach ($list as $v) {
            $img = "https://pic.7881.com/7881/market/images/game_logo/{$v['id_7881']}.png";
            $imgs[$v['id']] = $img;
            echo "<img src='$img'>";
        }
        //print_r($imgs);
        exit;
    }

    //图片根据id命名
    private function name() {
        $list = db('game_x')->where('level', 2)->select()->toArray();
        foreach ($list as $v) {
            copy(CMF_ROOT . 'xx/' . $v['id_7881'] . '.png', CMF_ROOT . 'xxx/' . $v['id'] . '.png');
        }
        //print_r($imgs);
        exit;
    }

    //设置图片
    private function tttt() {
        $list = db('game_bac')->where('level', 1)->select()->toArray();
        foreach ($list as $v) {
            $item = db('game_bac')->where(['level' => 2, 'parent_id' => $v['id']])->order('id ASC')->find();
            db('game_bac')->where('id', $v['id'])->update(['thumb' => $item['thumb']]);
            print_r($item);
        }
        //print_r($imgs);
        exit;
    }

    function getId($id) {
        $item = db('game_bac')->where('parent_id', $id)->order('id DESC')->find();
        if ($item) {
            return $this->getId($item['id']);
        } else {
            return $id;
        }
    }

    private function ttttx() {//列出二级分类的尾分类
        $list = db('game_bac')->where('level', 2)->select()->toArray();
        foreach ($list as $l) {
            $id = $this->getId($l['id']);
            print_r($id);
            echo ",";

        }
        exit;
    }

    private function getField() {//采集7881的附加字段
        $type = db('goods_type_bac')->where('list_order', 0)->find();
        if ($type) {
            $typeId = $type['id'];
            db('goods_type_bac')->where('id', $typeId)->update(['list_order' => 1]);
            $game = db('game_bac')->where('id', $type['game_id'])->find();
            $game = db('game_bac')->where('parent_id', $game['id'])->find();
            $gameId = $game['id_7881'];
            $data = json_decode(file_get_contents("https://gw.7881.com/basic/api/publish-item?gameId=$gameId&gtId=100003"), true);
            $data = $data['body'];
            if ($data) {
                $dataRadio = [];
                $dataCheckbox = [];
                $dataText = [];
                foreach ($data as $k => $v) {
                    if ($v['gname'] != '账号属性信息') continue;
                    if ($v['type'] == 'select') {
                        $dataRadio[] = $v;
                    } elseif ($v['type'] == 'checkbox') {
                        $dataCheckbox[] = $v;
                    } else {
                        $dataText[] = $v;
                    }
                }
                $data = array_merge($dataRadio, $dataText, $dataCheckbox);
                if ($data) {
                    $arr = [];
                    foreach ($data as $k => $v) {
                        if ($k > 19) break;
                        $fieldName = 'f' . ($k + 1);
                        $type = 'text';
                        $options = '';
                        $required = '';
                        if ($v['must'] == 1) {
                            $required = '1';
                        }
                        if ($v['type'] == 'select') {
                            $type = 'radio';
                            $options = $v['newValue'];
                        } elseif ($v['type'] == 'checkbox') {
                            $type = 'checkbox';
                            $options = $v['newValue'];
                        }
                        $options = str_replace(',', ' ', $options);
                        $arr[$fieldName] = [
                            "name" => $v['keyname'],
                            "type" => $type,
                            "options" => $options,
                            "required" => $required
                        ];
                    }
                    print_r($data);
                    if ($arr) {
                        db('goods_type_field')->insert([
                            'type_id' => $typeId,
                            'data' => json_encode($arr, JSON_UNESCAPED_UNICODE)
                        ]);
                    }
                    print_r(json_encode($arr, JSON_UNESCAPED_UNICODE));
                }

            }
        }
        echo '<script>//setTimeout(location.reload(), 50)</script>';
        exit;
    }

    private function xx666() {//添加测试商品
        $str = '936,938,6,8,10,940,943,1004,1016,1043,22,1091,1113,1125,1176,1184,1197,4821,4829,4832,4835,4839,4843,4854,4856,4862,4865,4875,4892,5009,5056,5104,5110,5159,5558,5591,5719,5769,5808,78,5810,82,5818,86,5820,5836,5839,5995,6058,6207,6226,6310,6435,6584,6590,16645,16685,111,16733,16784,16832,119,16840,16853,16962,16983,17001,17112,17116,17998,21666,21668,21672,21676,21680,146,21699,21727,21733,154,21781,21829,21854,21861,21987,21995,22004,22006,22012,22250,22260,22263,22269,22302,22330,23267,186,23318,23367,192,23369,23382,23391,23394,23397,23400,23406,23412,24032,26719,26767,27385,28425,28434,220,28482,28530,28578,28671,28717,28719,28721,28723,28733,28751,29164,29437,29780,29843,29874,29905,29912,29919,30119,30121,30124,30401,30503,30561,31713,31761,31770,31782,31971,32152,32194,32237,32263,285,287,32286,32291,32557,32761,32768,32775,32778,33174,33213,33369,33420,311,33471,33476,33527,33578,33597,33603,33611,33624,33654,34119,333,34287,336,34290,34296,34314,34364,345,34367,349,34687,352,35142,35176,35584,35682,35724,35738,35835,35937,36018,36585,36774,36782,36791,36839,381,383,36890,37680,38335,38386,38863,38939,38953,38955,38998,39015,39034,39068,39070,411,39076,415,39091,39094,39096,39131,39133,427,39135,39174,39268,39271,39277,39281,39313,50000,50036,50039,50693,50703,50834,50843,51420,51426,51432,53452,53478,467,53489,55490,472,474,55495,56151,56524,481,56530,56533,56539,56870,490,56872,56878,56880,57079,57206,57209,57313,58701,58907,58921,59377,59381,515,517,59393,59439,59469,59472,526,59502,59509,59512,59520,59579,59627,59675,59777,59912,59963,59982,59994,60069,60213,60229,60430,60546,60576,564,60915,567,568,60969,61139,62769,62771,577,579,581,62774,62778,62793,63001,63047,63181,63293,63298,63486,63492,63498,63899,64056,64062,64074,64158,64160,64208,64256,64273,64396,64400,64445,64473,630,65284,68176,68980,637,639,68985,68988,68995,69052,69060,69102,69420,69605,70097,70340,70566,70568,70581,71401,71693,71702,72320,72452,74232,74280,74328,74706,74718,74764,75005,75159,75175,75177,693,75236,75240,78338,78342,78453,703,705,707,78786,78809,79047,79100,79144,79946,79948,79967,80029,80110,80303,80351,80399,80566,80575,80718,80747,80797,80799,80854,80897,81067,81084,81164,81213,84741,89889,762,764,91066,767,769,771,91116,91135,91140,91164,91168,91170,91277,91359,91442,91694,91823,92096,92191,92267,92305,92337,92340,92347,92373,92431,92458,92464,92474,92478,92548,92554,92561,92681,92885,92933,92947,830,92957,93008,93020,93326,93348,93387,93390,93403,93430,93457,93634,93640,93678,93744,859,93750,93753,93790,93839,93849,93874,93922,93970,876,94021,94082,94289,94305,94313,94339,94401,94416,894,895,94422,94429,94431,903,94558,94686,94693,94712,94714,94717,94799,95011,95377,95426,95434,95442,95455,95457,95462';
        $xxx = '账号';
        foreach (explode(',', $str) as $id) {
            $data = [
                'game_id' => $id,
                'create_time' => $this->time,
                'status' => 3,
                'user_id' => 10,
                'title' => '【测试商品】蓝风游戏交易系统 - 我们只做游戏交易系统，所以我们更专业【一次购买，终身更新】',
                'bright_spot' => '蓝风游戏交易系统 售后无忧',
                'type_name' => $xxx,
                'delivery_type' => 1,
                'delivery_type_name' => '平台代发',
                'description' => "蓝风游戏交易系统是百川网络（聊城开发区百川网络服务有限公司）推出的一款专业的游戏账号交易平台！\n平台包含多种功能模块。还可开发定制，满足您的一切需求。",
                'quantity' => $xxx == '金币' ? 1000 : 1,
                'quantity_sold' => 0,
                'inventory_quantity' => $xxx == '金币' ? 1000 : 1,
                'trading_time' => '0点到24点',
                'trading_time_start' => 0,
                'trading_time_end' => 24,
                'mobile' => '18888888888',
                'qq' => 12345678,
                'wechat' => 'lanfeng666',
                'expiration_day' => 0,
                'account_fields' => '[{"name":"账号","type":"input","value":"lanfeng666666"},{"name":"密码","type":"input","value":"lanfeng888888"},{"name":"角色名称","type":"input","value":"蓝风游戏交易系统"}]'
            ];
            $game = db("game")->where("id", $data["game_id"])->find();
            $gameIds = explode(",", $game["parent_ids"]);
            array_push($gameIds, $data["game_id"]);
            $data["game_ids"] = implode(",", $gameIds);
            $gameParents = [];
            foreach ($gameIds as $v) {
                $gameParents[] = db("game")->field("id, name")->where("id", $v)->find();
            }
            $data["game_id_level_1"] = $gameParents[0]["id"];
            $data["game_name_level_1"] = $gameParents[0]["name"];
            array_shift($gameParents);
            $data["game_server"] = implode("/", array_column($gameParents, "name"));
            $data["type"] = db("goods_type_bac")->where([
                ["name", '=', $xxx],
                ["game_id", '=', $data["game_id_level_1"]],
            ])->find()['id'];

            for ($i = 0; $i < 100; $i++) {
                $imgs = [];
                for ($ii = 0; $ii < 5; $ii++) {
                    $imgs[] = '/static/demo/image/goods/' . rand(1, 2000) . '.jpg';
                }
                $data["image"] = implode(',', $imgs);
                $data['price'] = rand(5, 120) . [6, 8][rand(0, 1)];
                if ($xxx == '金币') {
                    $data['price'] = rand(5, 80) . [6, 8][rand(0, 1)];
                }
                db('goods')->insert($data);
            }
            //print_r($data);

            //exit;

        }
        exit;
    }


}
